--[[
    NuckPluck - An android game for learning the Thai alphabet
    Copyright (C) 2013 Nathan Kieffer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Contact: nathankieffer@gmail.com
--]]

local Vector = require("vector")

local Controller = {}

function Controller.newController()
   local controller = display.newGroup()
   controller.pad = display.newCircle(controller,0,0,500)
   controller.pad:setFillColor(1,.01)

   local point = display.newCircle(controller, 0,0,5)

   --controller:setReferencePoint(display.CenterReferenctPoint)

   --movex and movey are generated in the vector from the center of the 
   --control pad to the point of touch.
   --used to control the movement of the player and the map.
   controller.movex = 0
   controller.movey = 0

   --step is derived from the magnitude of the vector
   controller.step = 0
   controller.magnitude = 0
   controller.speedFactor = 10
   --moving stores whether or not the controller is being touched
   controller.moving = false

   function controller:setPosition(x, y)
      self.x = x
      self.y = y
   end

   function controller:setSize(width)
      self.pad.width = width
      self.pad.height = width
   end

   function controller:touch(event)
      if event.phase == "ended" or event.phase == "cancelled"  then
	 print("outside")
	 self.magnitude = 0
	 self.moving = false
	 --self.movex = 0
	 --self.movey = 0
	 return true
      end

      if event.phase == "began" then
	 self.moving = true
	 local v =  Vector.fromPoints({x=self.x, y=self.y},
				      {x=event.x, y=event.y})
	 self.magnitude = v:getMagnitude()
--	 self.gaugeDot = display.newCircle(self, event.x - self.x, event.y - self.y, 5)
--	 self.gaugeDot:setFillColor(1,0,0)
	 self.step = math.round(self.magnitude/self.speedFactor)
	 v:normalize()
	 self.movex = -v.x
	 self.movey = -v.y
	 self.angle = v:toAngle()
      end
      if event.phase == "moved" then
	 local v =  Vector.fromPoints({x=self.x, y=self.y},
				      {x=event.x, y=event.y})
	 self.magnitude = v:getMagnitude()
--	 self.gaugeDot.x = event.x - self.x
--	 self.gaugeDot.y = event.y - self.y
	 self.step = math.round(self.magnitude/self.speedFactor)
	 v:normalize()
	 self.movex = -v.x
	 self.movey = -v.y
	 self.angle = v:toAngle()

      end
   end

   function controller:slowDown()
      --This function eases the speed the player moves at down. 
      if self.step > 0 and not self.moving then
	 self.step = self.step - .5
      end
   end
   
   controller:addEventListener("touch", controller)
 --  controller:setSize(200)
   return controller
end

return Controller